<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'
import { getConsants } from '@/utils'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['edit', 'xq', 'pageChange', 'reload'])

// 表格列配置
const columns = ref([
  { ellipsis: true, title: '工单编号', dataIndex: 'orderNo', key: 'orderNo' },
  { ellipsis: true, title: '耕作类型', key: 'orderType' },
  { ellipsis: true, title: '农机名称', dataIndex: 'machineName', key: 'machineName' },
  { ellipsis: true, title: '耕作地点', key: 'farmingAddress' },
  {
    ellipsis: true,
    title: '实际作业面积（亩）',
    dataIndex: 'realityFarmingArea',
    key: 'realityFarmingArea'
  },
  {
    ellipsis: true,
    title: '实际单价(元)',
    dataIndex: 'realityFarmingPrice',
    key: 'realityFarmingPrice'
  },
  {
    ellipsis: true,
    title: '实际金额(元)',
    dataIndex: 'finalSettlementMoney',
    key: 'finalSettlementMoney'
  },
  { ellipsis: true, title: '结算状态', key: 'orderStatus1' }
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}

// 数据详情
const xq = (item) => {
  emits('xq', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除工单',
    content: h(
      'span',
      {},
      '是否删除工单：',
      h('span', { style: { color: '#09c690' } }, item.orderNo)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delFar({ id: item.deviceId })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'orderType'">
        {{ getConsants('machine_type', record.orderType) }}
      </template>
      <template v-if="column.key === 'orderStatus1'">
        {{ getConsants('order_status1', record.orderStatus) }}
      </template>
      <template v-if="column.key === 'farmingAddress'">
        {{ record.details[0].farmingAddress }}
      </template>
      <template v-if="column.key === 'unitPrice'">
        {{ record.minPreSettlementMoney }} ~ {{ record.minPreSettlementMoney }}
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="xq(record)">详情</AButton>
        <AButton type="link" @click="edit(record)">编辑</AButton>
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
    </template>
  </MyTable>
</template>
